Skip to content

resolved_class_annotations: resolved once per class and kept - #29

Open
trissim wants to merge 1 commit into
mainfrom
class-annotations-once
Open

trissim wants to merge 1 commit into
mainfrom
class-annotations-once

Conversation

@trissim

@trissim trissim commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Summary

resolved_class_annotations now resolves once per class and keeps the result.

A class's resolved annotations are a fact about the class. Before, every validated dataclass instance (AnnotatedDataclassValidationMixin.__post_init__ → validate_annotated_dataclass) resolved them again: get_type_hints plus compiling each forward reference. In Toad's Qt client that was most of building a nested settings object.

  • The cache. A WeakKeyDictionary maps each class to a read-only MappingProxyType of its annotations.
  • Unresolved classes. A class whose annotations don't resolve yet raises NameError and nothing is kept, so a later call resolves it.
  • jsonable. Its own per-class copy of this cache (_FIELD_TYPES_BY_DATACLASS) is removed; it reads the owner's.

Measured in Toad's Qt client

  • Sidebar drag release: #2107's qt_drive drag_speed, at phone speed (45% of one core), 3 runs per tree, median. #2107 attributed 0.54 s of its drag's 0.70 s of settings writes to this validation.
    • Installed libraries: 420 ms (405, 668, 420).
    • This PR with objectstate's companion PR: 197 ms (222, 197, 170).
  • A view's settings state, built as Toad's ObjectConfiguration builds it (unthrottled, median of 5):
    • 10.5 → 4.6 ms with this PR alone.
    • 3.5 ms with objectstate's companion PR as well.

Tests

  • 196 passed (main: 194).
  • New tests/test_class_annotation_cache.py:
    • The same read-only mapping is returned on every call.
    • A class that doesn't resolve yet is resolved on a later call.

🤖 Generated with Claude Code

https://claude.ai/code/session_01RnH6dT7wdh7i4tjJZQdFu3

A class's resolved annotations are a fact about the class. Every validated
dataclass instance asks for them (AnnotatedDataclassValidationMixin), and
resolving them again for each instance (get_type_hints, compiling each
forward reference) was most of building a nested settings object: in
Toad's Qt client at a phone's speed, 0.54 s of a sidebar drag's 0.70 s of
settings writes, and about half of opening a view's configuration.

resolved_class_annotations keeps the answer per class (a WeakKeyDictionary,
read-only), and jsonable's own per-class copy of it goes. A class whose
annotations do not resolve yet raises and keeps nothing, so a later call
resolves it then.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RnH6dT7wdh7i4tjJZQdFu3

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant